/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Category.java
 *
 * Created on Mar 23, 2011, 4:56:07 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.*;
import vn.aptech.project.model.dto.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr.Hiep
 */
public class Category extends java.awt.Dialog {

    /** Creates new form Category */
    public Category(java.awt.Frame parent, boolean modal) throws IOException {
        super(parent, modal);
        initComponents();
        txtTypeEdit.setEnabled(false);
        bntEditCate.setEnabled(false);
        this.setTitle("Category");
        SelectCategory();
    }

    public void SelectCategory() throws IOException {
        tblcategory.setComponentPopupMenu(PopCategory);
        try {
            category_BLL cgb = new category_BLL();
            Vector vt = cgb.selectcategory();
            Vector vtCol = new Vector();
            vtCol.add("Id");
            vtCol.add("Department");
            DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

                @Override
                public boolean isCellEditable(int rowIndex, int mColIndex) {
                    return false;
                }
            };
            tblcategory.setModel(dtm);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PopCategory = new javax.swing.JPopupMenu();
        MnCategoryDelete = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblcategory = new javax.swing.JTable();
        txtTypeinfo = new javax.swing.JTextField();
        txtTypeEdit = new javax.swing.JTextField();
        bntCreate = new javax.swing.JButton();
        bntEditCate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        MnCategoryDelete.setText("Delete");
        MnCategoryDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MnCategoryDeleteActionPerformed(evt);
            }
        });
        PopCategory.add(MnCategoryDelete);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        tblcategory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblcategory.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblcategoryMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblcategory);

        txtTypeinfo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204)));

        txtTypeEdit.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204)));

        bntCreate.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntCreate.setForeground(new java.awt.Color(51, 0, 255));
        bntCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        bntCreate.setText("Create");
        bntCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCreateActionPerformed(evt);
            }
        });

        bntEditCate.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntEditCate.setForeground(new java.awt.Color(51, 51, 255));
        bntEditCate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.png"))); // NOI18N
        bntEditCate.setText("Edit");
        bntEditCate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEditCateActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Category");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtTypeinfo, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(bntCreate)
                .addGap(20, 20, 20))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtTypeEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(bntEditCate)
                .addGap(40, 40, 40))
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTypeinfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bntCreate)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bntEditCate)
                            .addComponent(txtTypeEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void tblcategoryMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblcategoryMouseClicked
        int currentline = tblcategory.getSelectedRow();
        if (currentline > -1) {
            txtTypeEdit.setEnabled(true);
            bntEditCate.setEnabled(true);
            txtTypeEdit.setText(tblcategory.getValueAt(currentline, 1).toString());
            variable.CateTypeid = Integer.parseInt(tblcategory.getValueAt(currentline, 0).toString());
            variable.CateTypeinfo = tblcategory.getValueAt(currentline, 1).toString();
        }
}//GEN-LAST:event_tblcategoryMouseClicked

    private void bntCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCreateActionPerformed
        try {
            if (txtTypeinfo.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Department not blank !!!");
                txtTypeinfo.requestFocus();
                return;
            }
            category_DTO cd = new category_DTO();
            cd.setTypeinfo(txtTypeinfo.getText());
            category_BLL cb = new category_BLL();
            cb.insertcategory(cd);
            SelectCategory();
        } catch (IOException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_bntCreateActionPerformed

    private void bntEditCateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEditCateActionPerformed
        try {
            category_DTO ctd = new category_DTO();
            ctd.setTypeid(variable.CateTypeid);
            ctd.setTypeinfo(txtTypeEdit.getText());
            category_BLL ctb = new category_BLL();
            ctb.updatecategory(ctd);
            SelectCategory();
            txtTypeEdit.setEnabled(false);
            bntEditCate.setEnabled(false);
        } catch (IOException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntEditCateActionPerformed

    private void MnCategoryDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MnCategoryDeleteActionPerformed
        int reply = JOptionPane.showConfirmDialog(this, "Sure you delete", null, JOptionPane.OK_CANCEL_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            int currentline = tblcategory.getSelectedRow();
            try {
                if (currentline < 0) {
                    JOptionPane.showMessageDialog(this, "Select a line");
                } else {
                    int a = Integer.parseInt(String.valueOf(tblcategory.getValueAt(currentline, 0)));
                    product_BLL pb = new product_BLL();
                    Vector vt = pb.selectProductByTypeid(a);
                    if (vt.isEmpty()) {
                        category_BLL cb = new category_BLL();
                        cb.deletecategory(a);
                        SelectCategory();
                        JOptionPane.showMessageDialog(this, "Successful Delete");
                    } else {
                        JOptionPane.showMessageDialog(this, "Cannot delete as subdirectories still exist");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_MnCategoryDeleteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    Category dialog = new Category(new java.awt.Frame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem MnCategoryDelete;
    private javax.swing.JPopupMenu PopCategory;
    private javax.swing.JButton bntCreate;
    private javax.swing.JButton bntEditCate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tblcategory;
    private javax.swing.JTextField txtTypeEdit;
    private javax.swing.JTextField txtTypeinfo;
    // End of variables declaration//GEN-END:variables
}
